/*
 * @Author: jianglijun 
 * @Date: 2019-02-27 05:15:56 
 * @Last Modified by: jianglijun
 * @Last Modified time: 2019-02-28 01:10:30
 */
var express = require('express');
var router = express.Router();
var User = require("../models/user");
var utils = require("../comm/utils");
// ------------- 获取地址列表 -------- //
router.get("/addressList",(req,res,next) => {
    let userId = +req.cookies.userId;
    User.findOne({userId},(err,doc) => {
      if(err){
        res.json({
          errno:"1",
          msg:err.message,
          data:err.message
        })
      }else{
          doc.addressList.sort(utils.sortBy('isDefault',true,parseInt));// 对数组排序 把默认值设置为第一个
          res.json({
            errno:0,
            msg:"成功",
            data:doc.addressList
          })
      }
    })
  })
// ------------- 设置默认地址 ------- //
router.post("/setaddress", function (req,res,next) {
    var userId = req.cookies.userId,
      addressId = req.body.addressId;
      if(!addressId){
        res.json({
          status:'1004',
          msg:'addressId is null',
          result:''
        });
      }else{
        User.findOne({userId},(err02,doc02) =>{
            if(err02){
                res.json({errno: 1, msg:err02.message});
                return;
            } else {
                if(doc02){
                    let objnew = doc02.addressList.find(item => item.isDefault ===1 );
                   if(!objnew){
                    User.update(
                        {
                            userId,
                            "addressList.addressId":addressId
                        },
                        {
                            "addressList.$.isDefault":1,
                        },
                        (err01,doc01) => {
                            if(err01){
                                res.json({errno: 1, msg:err01.message});
                                return;
                            }else{
                                res.json({
                                    errno:0,
                                    msg:"",
                                    data:doc01
                                })
                            }
                        }
                    )

                   }else{
                        User.update(
                            {
                                userId,
                                "addressList.addressId":addressId
                            },
                            {
                                "addressList.$.isDefault":1,
                            },
                            (err01,doc01) => {
                                if(err01){
                                    res.json({errno: 1, msg:err01.message});
                                    return;
                                }else{
                                    User.update(
                                        {
                                            userId,
                                            "addressList.addressId":objnew.addressId
                                        },
                                        {
                                            "addressList.$.isDefault":0,
                                        },
                                        (err001,doc001) => {
                                            if(err001){
                                                res.json({errno: 1, msg:err001.message});
                                                return;
                                            }else{
                                                res.json({
                                                    errno:0,
                                                    msg:"",
                                                    data:doc001
                                                })
                                            }
                                        }
                                    )
                                }
                            }
                        )
                   }
                }
            }
        })
      }
})
// ------------ 删除地址 ---------- //
router.post("/delAddress",(req,res,next) => {
    var userId = req.cookies.userId,
        addressId = req.body.addressId;
    User.update({userId},
        {
            $pull:{
                "addressList":{
                    addressId
                }
            }
        },
        (err,doc) => {
            if(err){
                res.json({errno: 1, msg:err.message});
                return;
            }else{
                let {nModified,ok} = doc;
                if(nModified==1 && ok==1){
                    res.json({
                        erron:0,
                        msg:"删除成功",
                        data:"删除成功"
                    })
                }
                else{
                    res.json({
                        erron:0,
                        msg:doc,
                        data:""
                    })
                }
            }
        }
    )
})
// ---------- 添加地址  --------- //
router.post("/addAddress",(req,res,next) => {
    var userId = req.cookies.userId,
        userName = req.body.userName,
        streetName = req.body.streetName,
        postCode = req.body.postCode || '',
        tel = req.body.tel || '',
        isDefault = req.body.isDefault || 0,
        addressId = utils.mathBy();
        User.findOne({userId},(err,doc) => {
            if(err){
                res.json({
                    errno:1,
                    msg:doc.message
                })
            }
            else {
                if(doc){
                    doc.addressList.forEach(ele => {
                        ele.isDefault = 0;
                    });
                    doc.addressList.push({addressId,userName,streetName,postCode,tel,isDefault});
                    doc.save((err,doc2) => { 
                        if(err){
                            res.json({
                                errno:1001,
                                msg:err.message
                            }) 
                        } 
                        else {
                            res.json({
                                errno:0,
                                msg:"成功",
                                data:"成功"
                            })
                        }
                    })
                }
            }
        })
        
})
module.exports = router;